
#include "shape_manager.h"

namespace vgl{


std::map<shape_type_t,stencil_t *> ShapeManager::m_stencils ;
ShapeManager *ShapeManager::self = NULL;

ShapeManager::ShapeManager(){
}


ShapeManager::~ShapeManager(){
	
	close_gdiplus();
	delete_map(m_stencils);

}


//should we use DESTROY_SINGLETON instead
void ShapeManager::close(){
	if(self) delete self;
}

bool ShapeManager::register_stencil(shape_type_t t,stencil_t *stencil){

	assert(stencil);
	if(m_stencils.find(t) != m_stencils.end()){
		log("a shape of the same type already defined");
		return false;
	}

	m_stencils.insert(std::make_pair(t,stencil));

	return true;
}

stencil_t *ShapeManager::get_stencil(shape_type_t  t){

	stencil_t * s = NULL;

	std::map<shape_type_t,stencil_t *>::iterator it= m_stencils.find(t);

	assert(it != m_stencils.end());

	if(it != m_stencils.end()){
		s = it->second;
	}

	return s;
}


static GdiplusStartupInput gdiSI;
static GdiplusStartupOutput gdiSO;
static ULONG_PTR gdiToken;

void ShapeManager::init_gdiplus(){
	
	ULONG_PTR gdiHookToken;

	gdiSI.SuppressBackgroundThread = TRUE;
	GdiplusStartup(&gdiToken,&gdiSI,&gdiSO);
}


void ShapeManager::close_gdiplus(){
	GdiplusShutdown(gdiToken);
}

void ShapeManager::init_all(){

	init_gdiplus();
	init_rectangle_stencil();
	init_annotation1_stencil();
}
	
void ShapeManager:: init_rectangle_stencil(){

	stencil_t *s = new stencil_t();
	//left-top
	s->add_ctrl_point(ctrl_resize,point_t(0.0f, 0.0f));
	//top right
	s->add_ctrl_point(ctrl_resize,point_t(1.0f, 0.0f));
	//bottom right
	s->add_ctrl_point(ctrl_resize,point_t(1.0f, 1.0f));
	//left -bottom
	s->add_ctrl_point(ctrl_resize,point_t(0.0f, 1.0f));

	//todo :add 4 center point also
	s->add_ctrl_point(ctrl_resize,point_t(0.5f, 0.0f));
	s->add_ctrl_point(ctrl_resize,point_t(1.0f, 0.5f));
	s->add_ctrl_point(ctrl_resize,point_t(0.5f, 1.0f));
	s->add_ctrl_point(ctrl_resize,point_t(0.0f, 0.5f));

	register_stencil(shape_rectangle,s);
}


void ShapeManager::init_annotation1_stencil(){

	
	
	

	//register_stencil(shape_annotation1,path);
}
};